#include "Game.h"
using namespace core;

Game::Game()
{
	if (Init())
	{
		InitGuiEnvironment();
	}
}

Game::~Game()
{
}

bool Game::Init()
{
	device = createDevice(EDT_SOFTWARE, dimension2d<u32>(1024, 720), 24, false, false, false, 0);
	if (!device)
		return false;

	device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
	
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	return true;
}

bool Game::InitGuiEnvironment()
{
	guienv = device->getGUIEnvironment();
	GuiCreator::initGuiCreator(guienv, device);
	GuiCreator::createHUD();
	device->setEventReceiver(GuiCreator::getEventReceiver());

	return true;
}

void Game::Start()
{
	isRunning = true;
	gameState = INGAME;
	Loop();
}

void Game::Loop()
{
	while (isRunning && device->run())
	{
		switch (gameState)
		{
		case INGAME:
		{
					   driver->beginScene();
					   smgr->drawAll();
					   driver->draw2DImage(Assets::background, core::position2d<s32>(0, 0),
						   core::rect<s32>(0, 0, 1280, 720), 0,
						   video::SColor(255, 255, 255, 255), false);
					   guienv->drawAll();
					   driver->endScene();
		}
			break;
		case MAINMENU:
		{
						 driver->beginScene();
						 guienv->drawAll();
						 driver->endScene();
		}
			break;
		}

		device->drop();
	}
}


void Game::addSpriteForRendering(Sprite* sprite)
{
	sprites.push_back(sprite); 
}

